﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Text;
using OPCDiffCore;
using OPCDiffCmd.Cartridge;

namespace OPCDiffCmd
{
    class Program
    {
        static void Main(string[] args)
        {
            // Check program arguments
            if (args.Length < 3 || args.Length > 4)
            {
                PrintUsage();
                return;
            }

            // TODO : Mettre un parseur de ligne de commande !
            string originalFile = args[0];
            string diffFile = args[1];
            string diffLevel = args[2];
            string outputStyle = args[3];

            // Check input files
            if (originalFile.ToLowerInvariant() == diffFile.ToLowerInvariant())
            {
                Console.WriteLine("You can't select the same file as original and target document !");
                Console.ReadLine();
                return;
            }

            if (!File.Exists(originalFile))
            {
                Console.WriteLine("The reference file must exist !");
                Console.ReadLine();
                return;
            }

            if (!File.Exists(diffFile))
            {
                Console.WriteLine("The diff file must exist !");
                Console.ReadLine();
                return;
            }

            OPCDiffer packageComparer = new OPCDiffer();
            PackageDiff pkgDiff = packageComparer.GetPackageDiff(
                originalFile, diffFile, GetAnalyzeLevelFromArg(diffLevel));

            // TODO : le mettre en dynamique par rapport aux arguments
            IOutputCartridge outputCartridge = GetOutputCartridgeFromParameters(outputStyle);
            outputCartridge.BuildOutput(pkgDiff);
        }

        static IOutputCartridge GetOutputCartridgeFromParameters(string outputStyleArg)
        {
            switch (outputStyleArg.ToLower())
            {
                case "-html":
                    return new HTMLCartridge();

                default:
                    return new ConsoleCartridge();
            }
        }


        static AnalyzeLevel GetAnalyzeLevelFromArg(string arg)
        {
            switch (arg.ToLower())
            {
                case "-p":
                case "-physical":
                    return AnalyzeLevel.Physical;

                case "-ql":
                case "-quick":
                    return AnalyzeLevel.QuickLogical;

                case "-d":
                case "-deep":
                    return AnalyzeLevel.QuickLogical;

                case "-l":
                case "-logical":
                default:
                    return AnalyzeLevel.Logical;
            }
        }

        static void PrintUsage()
        {
            Console.WriteLine(String.Format("OPC Diff vers.{0} - Julien Chable",
                    System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()));
            Console.WriteLine("Usage: <Original file path> <Diff file path> [[-ql|quick] | [-l|logical] | [-p|-physical] | [-d|-deep]] [-html]");
            Console.ReadLine();
        }
    }
}
